﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Xml;
using System.Collections.ObjectModel;
using Microsoft.Office.Core;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
using BusinessDB;
using BusinessService.proto;
using BusinessService;

namespace TemplateGenerator
{
    public class SpecAllGenerator : AbstractSpecGenerator
    {
        protected override string TemplateName
        {
            get { return "SPEC完全版打印"; }
        }

        protected override string ConvertToXml(ProdDB lighting)
        {
            StringBuilder sb = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(sb))
            {
                writer.WriteStartElement("灯具的相关表");
                Write(writer, lighting);
                writer.WriteEndElement();
                writer.Flush();
            }
            return sb.ToString();
        }

        private void Write(XmlWriter xmlWriter, ProdDB data)
        {
            ResourceProvider provider = new ResourceProvider(); 

            xmlWriter.WriteStartElement("基本信息");
            xmlWriter.WriteElementString("产品品牌", data.ProdLabel);
            xmlWriter.WriteElementString("产品系列", data.ProdSeries);

            ResItem clsItem = (from r in provider.ProductTypes
                               where r.ItemID== data.ProdClass
                               select r).SingleOrDefault();
            xmlWriter.WriteElementString("产品分类", clsItem != null ? clsItem.ItemName : string.Empty);
            xmlWriter.WriteElementString("规格型号", data.ProdVersion);

            ResItem orginItem = (from r in provider.Orgin
                                 where r.ItemID== data.ProdOrgin
                                 select r).SingleOrDefault();
            xmlWriter.WriteElementString("产地", orginItem != null ? orginItem.ItemName : string.Empty);

            ResItem placeItem = (from r in provider.UseArea
                                 where r.ItemID== data.ProdPlace
                                 select r).SingleOrDefault();
            xmlWriter.WriteElementString("使用场所", placeItem != null ? placeItem.ItemName : string.Empty);

            clsItem = (from r in provider.LightingType
                               where r.ItemID== data.AdpClass
                               select r).SingleOrDefault();
            xmlWriter.WriteElementString("光源种类", clsItem != null ? clsItem.ItemName : string.Empty);
            xmlWriter.WriteElementString("光源型号", data.AdpVersion);
            xmlWriter.WriteElementString("色温或颜色", data.AdpColor !=0 ? data.AdpColor.ToString() : string.Empty);
            xmlWriter.WriteElementString("显色性", data.AdpCri != 0 ? data.AdpCri.ToString() : string.Empty);
            xmlWriter.WriteElementString("光输出", data.AdpOutput != 0 ? data.AdpOutput.ToString() : string.Empty);
            xmlWriter.WriteElementString("插脚形式", data.AdpPins);

            xmlWriter.WriteElementString("材质说明", data.PhyMaterial);
            xmlWriter.WriteElementString("外观颜色", data.PhyColor);
            xmlWriter.WriteElementString("外形尺寸", data.PhySize);
            xmlWriter.WriteElementString("防护等级", data.PhyDefend);

            ResItem impactItem = (from r in provider.DefendType
                                  where r.ItemID== data.PhyImpact
                                  select r).SingleOrDefault();
            xmlWriter.WriteElementString("防撞击等级", impactItem != null ? impactItem.ItemName : string.Empty);
            xmlWriter.WriteElementString("产品重量", data.PhyWeight != 0 ? data.PhyWeight.ToString() : string.Empty);

            ResItem explainItem = (from r in provider.LightingExplain
                            where r.ItemID== data.OptExplain
                            select r).SingleOrDefault();
            xmlWriter.WriteElementString("配光说明", explainItem != null ? explainItem.ItemName : string.Empty);
            xmlWriter.WriteElementString("光束角", data.OptAngle);
            xmlWriter.WriteElementString("灯具效率", data.OptEff != 0 ? data.OptEff.ToString() : string.Empty);

            xmlWriter.WriteElementString("防触电等级", data.EleShock);

            ResItem voltageItem = (from r in provider.WorkVoltage
                                   where r.ItemID== data.EleVoltage
                                   select r).SingleOrDefault();
            xmlWriter.WriteElementString("工作电压", voltageItem != null ? voltageItem.ItemName : string.Empty);

            xmlWriter.WriteElementString("电器类型", data.EleType);
            xmlWriter.WriteElementString("电器功耗", data.ElePower != 0 ? data.ElePower.ToString() : string.Empty);
            xmlWriter.WriteElementString("光源功率", data.EleLgtPower != 0 ? data.EleLgtPower.ToString() : string.Empty);
            xmlWriter.WriteElementString("整灯功耗", data.EleAllPower !=0 ? data.EleAllPower.ToString() : string.Empty);

            ResItem controlItem = (from r in provider.ControlType
                                   where r.ItemID== data.EleCtlType
                                   select r).SingleOrDefault();
            xmlWriter.WriteElementString("控制方式", controlItem != null ? controlItem.ItemName : string.Empty);

            ResItem setupItem = (from r in provider.SetupType
                            where r.ItemID== data.ProjSetup
                            select r).SingleOrDefault();
            xmlWriter.WriteElementString("安装方式", setupItem != null ? setupItem.ItemName : string.Empty);
            xmlWriter.WriteElementString("安装尺寸", data.ProjSize);
            xmlWriter.WriteElementString("认证及检测报告", data.ProjReport);
            xmlWriter.WriteElementString("备注", data.ProjBaikup);
            xmlWriter.WriteElementString("可选配件", data.ProjParts);

            xmlWriter.WriteElementString("参考进价", data.BusIn != 0 ? data.BusIn.ToString() : string.Empty);
            xmlWriter.WriteElementString("市场参考价", data.BusMarket != 0 ? data.BusMarket.ToString() : string.Empty);
            xmlWriter.WriteElementString("参考售价", data.BusOut !=0 ? data.BusOut.ToString() : string.Empty);
            xmlWriter.WriteElementString("参考货期", data.BusExpires);
            xmlWriter.WriteEndElement();
        }
    }
}
